package com.group.weekmgr;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

import com.group.util.JdbcUtil;

public class WeekDataMgr {
	Connection conn;
	PreparedStatement pstmt;
	ResultSet rs;
	DataSource ds;
	
	public WeekDataMgr() {
		try {
			//	pooling 처리 : dbcp
			Context context = new InitialContext();
			ds = (DataSource)context.lookup("java:comp/env/jdbc/Oracle10g");
		} catch (Exception e) {
			System.out.println("연결 오류 : " + e.getMessage());
			return;
		}
	}
	
	public boolean addMyWeekData(WeekDataBean bean,int week_flag){
		boolean b = false;
		String sql = "INSERT INTO WKDATA VALUES(?,sysdate,?,?,?,?,?,?,?,?)";
		try {
			conn = ds.getConnection();
			for(int i=0; i<bean.getWork_code().length; i++){
				pstmt = conn.prepareStatement(sql);
				pstmt.setString(1, bean.getId());
				pstmt.setInt(2, week_flag);		//	지난주 : 0, 금주 : 1
				pstmt.setString(3, bean.getWork_code()[i]);
				pstmt.setString(4, bean.getSale_memo()[i]);
				pstmt.setString(5, bean.getActivity_spec()[i]);
				pstmt.setInt(6, bean.getCheck_date());
				pstmt.setString(7, bean.getTarget_date()[i]);
				pstmt.setInt(8, bean.getTarget_ratio());
				pstmt.setInt(9, bean.getAct_ratio());
			
		
				System.out.println("toString() : " + bean.toString());
				
				int count = pstmt.executeUpdate();
				if(count == 1){
					b = true;
				}
			}
		} catch (Exception e) {
			System.out.println("addMyWeekData 오류 : " + e.getMessage());
		}finally{
			JdbcUtil.close(rs);
			JdbcUtil.close(pstmt);
			JdbcUtil.close(conn);
		}
		
		return b;
	}
}
